<?php
namespace app\index\service;

class EncryptService
{
    /**
     * 解密
     * @param string $encryptString
     * @return string
     */
    public function dataDecrypt($encryptString = '')
    {
        $decrypted = '';
        $key = "MIICXQIBAAKBgQDfCf6d/ut0Mt0FsIfBSdRsfNw8iEPomLztGlzvWn+9AzP/wGFVPZc55jbgrcKXvvRUGfpkqoN3xDyqa1qLxUJdw6rqVrwOmAn+c9t+jkzbxuAU3F9XVUykv29grODn0ZDSCc2+2ocYVTSZgVFAsqZ1it61ZrM7Cgzx1fWqTJqpbwIDAQABAoGAD2H5FZyHULRRWhsawdH9vJ2/8szTIi+mmBPBZSCwvrCB3Zm/HMnqrCucjVJuoViiMUq5YkgAWTpHlNlinVE1inEDGV3WReEDO3lJ2XHqUS49bMI3GIGrZTY6M0YcSKg1ygQTIWVA4ix4cCzMoks6OpjzPdVra7mbXvJPPCy3VoECQQD1p2XgjO5BZ60Tz+0naK86Vb7+JbEAU50Kvr0l1iE9qr+H2lEJKVBLcDSkU7XfUmEI2NzZZLKMdFJQeLyaNWNHAkEA6G7ES50EvWyZKV88daFks5XyNQBnwtq8ZlZjK4qec10cXsVbOBchcrzhnxjHjGObzc//Bx5wpREdnSbtBR0MmQJAO7+PY+JjQL06t4rnH6Kym/mnDqGUP4QWujpSPxWp2Bnjo+NRumy1/uM3qpBBuCONPl2gua9oTgkszC3RkaBSmQJBAMfEw/OVDcjcuLsFPop/X3S2UIjbQ1MG+tweLXfz2QGgKk0N5LvNrZLW4jgCbSetSPDYbwGoWPqWPlSeayJ0+CkCQQDdKb60shY108M44Gw4Z9rmqH+mWFdke+Kr99L+91yYHUI4/l++1/Vp/iqb7tWaScjR1tulYy196NH/6QxtO/We";
        $key_eol = (string) implode("\n", str_split((string) $key, 64));
        $privateKey = (string) "-----BEGIN RSA PRIVATE KEY-----\n" . $key_eol . "\n-----END RSA PRIVATE KEY-----";
        openssl_private_decrypt(base64_decode($encryptString), $decrypted, $privateKey);
        return $decrypted;
    }

    /**
     * 加密
     * @param string $data
     * @return string
     */
    public function dataEncrypt($data = '')
    {
        $encrypt_data = '';
        $key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDfCf6d/ut0Mt0FsIfBSdRsfNw8iEPomLztGlzvWn+9AzP/wGFVPZc55jbgrcKXvvRUGfpkqoN3xDyqa1qLxUJdw6rqVrwOmAn+c9t+jkzbxuAU3F9XVUykv29grODn0ZDSCc2+2ocYVTSZgVFAsqZ1it61ZrM7Cgzx1fWqTJqpbwIDAQAB";
        $key_eol = (string) implode("\n", str_split((string) $key, 64));
        $publicKey = (string) "-----BEGIN PUBLIC KEY-----\n" . $key_eol . "\n-----END PUBLIC KEY-----";
        openssl_public_encrypt($data, $encrypt_data, $publicKey);
        $encrypt_data = base64_encode($encrypt_data);
        return $encrypt_data;
    }
}